package com.example.loginpage.CloudSQL

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException

class MySQLConnection : ViewModel(){

    private val _loginResponse = MutableLiveData<ResultSet?>()
    val loginResponse: LiveData<ResultSet?> = _loginResponse

    private val _registerResponse = MutableLiveData<ResultSet?>()
    val registerResponse: LiveData<ResultSet?> = _registerResponse
    private fun getConnection(): Connection? {
        return try {
            MySQL.getConnection()
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
    // 从数据库中读取数据
    fun readSQL(sql: String,num: Int){
        viewModelScope.launch(Dispatchers.IO){
            try {
                val connection = getConnection()
                if (connection != null) {
                    val statement = connection.prepareStatement(sql)
                    val resultSet = statement.executeQuery()
                    withContext(Dispatchers.Main) {
                        when (num) {
                            1 -> {
                                _loginResponse.postValue(resultSet)
                            }
                            2 -> {
                                _registerResponse.postValue(resultSet)
                            }
                            else -> {
                                throw IllegalArgumentException("num must be either 1 or 2")
                            }
                        }
                    }
                }
            } catch (e: SQLException) {
                e.printStackTrace()
            }
        }
    }
    // 更新数据库的数据
    fun updateSQL(sql: String) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                val connection = getConnection()
                if (connection != null) {
                    val statement = connection.prepareStatement(sql)
                    val rowsAffected = statement.executeUpdate()
                    withContext(Dispatchers.Main) {
                        Log.i("插入结果",rowsAffected.toString())
                    }
                }
            } catch (e: SQLException) {
                e.printStackTrace()
            }
        }
    }

}

object MySQL {
    private const val DRIVER = "com.mysql.jdbc.Driver"
    private const val DBURL = "jdbc:mysql://rm-2zeaijwt45x4mb2s7yo.mysql.rds.aliyuncs.com:3306/user_credentials"
    private const val USER = "zhang"
    private const val PASSWORD = "zhang@123"
    init {
        println("dbURL: $DBURL")
    }
    @Throws(ClassNotFoundException::class, SQLException::class)
    fun getConnection(): Connection {
        Class.forName(DRIVER)
        return DriverManager.getConnection(DBURL, USER, PASSWORD)
    }
}